var historyAction = [];

var courseNameInputObject = $("input[name=course_name]");
var weekFromInputObject = $("input[name=week_from]");
var weekToInputObject = $("input[name=week_to]");
var daySelectorObject = $("select[name=day_selector]");
var timeFromInputObject = $("input[name=time_from]");
var timeToInputObject = $("input[name=time_to]");
var contentTextAreaObject = $("textarea[name=content]");

var current_course_name = courseNameInputObject.val();
var current_week_from = weekFromInputObject.val();
var current_week_to = weekToInputObject.val();
var current_day_selected = daySelectorObject.val();
var current_time_from = timeFromInputObject.val();
var current_time_to = timeToInputObject.val();
var last_content = contentTextAreaObject.val();

function contentChecker() {
    var currentContent = contentTextAreaObject.val();
    if (currentContent != last_content) {
        historyAction.push(["content", last_content]);
        last_content = currentContent;
        enableUndoButton();
    }
}

var contentCheckerTimer = setInterval(contentChecker, 5000);

function enableUndoButton() {
    $("button[name=undo_button]").prop("disabled", false);
}

function disableUndoButton() {
    $("button[name=undo_button]").prop("disabled", true);
}

function onUndoButtonClicked() {
    var lastAction = historyAction.pop();
    if (lastAction[0] === 'course_name')
    {
        courseNameInputObject.val(lastAction[1]);
        current_course_name = lastAction[1];
    }
    else if (lastAction[0] === 'week_from')
    {
        weekFromInputObject.val(lastAction[1]);
        current_week_from = lastAction[1];
    }
    else if (lastAction[0] === "week_to")
    {
        weekToInputObject.val(lastAction[1]);
        current_week_to = lastAction[1];
    }
    else if (lastAction[0] === "day_selector")
    {
        daySelectorObject.val(lastAction[1]);
        current_day_selected = lastAction[1];
    }
    else if (lastAction[0] === "time_from")
    {
        timeFromInputObject.val(lastAction[1]);
        current_time_from = lastAction[1];
    }
    else if (lastAction[0] === "time_to")
    {
        timeToInputObject.val(lastAction[1]);
        current_time_to = lastAction[1];
    }
    else if (lastAction[0] === "content")
    {
        contentTextAreaObject.val(lastAction[1]);
        last_content = lastAction[1];
    }
    if (historyAction.length === 0)
        disableUndoButton();
}


courseNameInputObject.change(
    function () {
        historyAction.push(['course_name', current_course_name]);
        current_course_name = $(this).val();
        enableUndoButton();
    }
);

weekFromInputObject.change(
    function () {
        historyAction.push(['week_from', current_week_from]);
        current_week_from = $(this).val();
        enableUndoButton();
    }
);

weekToInputObject.change(
    function () {
        historyAction.push(['week_to', current_week_to]);
        current_week_to = $(this).val();
        enableUndoButton();
    }
);

daySelectorObject.change(
    function () {
        historyAction.push(['day_selector', current_day_selected]);
        current_day_selected = $(this).val();
        enableUndoButton();
    }
);

timeFromInputObject.change(
    function () {
        historyAction.push(['time_from', current_time_from]);
        current_time_from = $(this).val();
        enableUndoButton();
    }
)

timeToInputObject.change(
    function () {
        historyAction.push(['time_to', current_time_to]);
        current_time_to = $(this).val();
        enableUndoButton();
    }
)